{extend name="public:head"}
{block name="main"}

<form class="layui-form" style="margin: 20px">
    <div class="demoTable">
        条件搜索：

        <div class="layui-inline">
            <select name="status">
                <option value="">全部状态</option>
                <option value="1">正常</option>
                <option value="0">禁用</option>
            </select>
        </div>

        <div class="layui-inline">
            <input class="layui-input" name="sign"  autocomplete="off" placeholder="渠道名称">
        </div>

        <div class="layui-inline">
            <button class="layui-btn" lay-filter="submit" lay-submit>搜索</button>
            <button class="layui-btn layui-btn-primary"  lay-filter="submit" lay-submit>重置</button>
        </div>

    </div>
</form>

<table class="layui-hide" id="all" lay-filter="all" lay-data="{id: 'sousuo'}"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
</script>

<script type="text/html" id="list">
    <a lay-event="pop" class="layui-btn layui-btn layui-btn-xs layui-btn-warm">流量</a>
    <a lay-event="reg" class="layui-btn layui-btn layui-btn-xs layui-btn-warm">注册</a>
</script>

<script type="text/html" id="host_d"></script>

<script type="text/html" id="host_f"></script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="disableSign">禁用</button>
        <button class="layui-btn layui-btn-sm" lay-event="noDisableSign">取消禁用</button>
    </div>
</script>

<script type="text/html" id="status">
    {{#  if(d.status == '1'){ }} <a class="layui-btn layui-btn-xs">启用</a> {{#  } }}
    {{#  if(d.status == '0'){ }} <a class="layui-btn layui-btn-xs layui-btn-danger">禁用</a> {{#  } }}
</script>



<script>
    layui.use(['table','layer','form'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var name = getQueryVariable('name')
        table.render({
            elem: '#all'
            ,url:'/admin/Sign/allSignApi?name='+ name
            ,toolbar: '#toolbarDemo'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'sign_id', title:'ID',  fixed: 'left', unresize: true, sort: true}
                ,{field:'channel', title:'渠道名称'}
                ,{field:'sign', title:'标识码'}
                ,{field:'pwd', title:'密码'}
                ,{field:'host_d', title:'单期域名' ,templet:'#host_d'}
                ,{field:'host_f', title:'分期域名' ,templet:'#host_f'}
                ,{field:'add_time', title:'添加时间'}
                ,{field:'update_time', title:'修改时间'}
                ,{field:'status', title:'状态', templet:'#status',event:"status",width: 80}
                ,{field:'list', title:'记录', templet:'#list', event:"list"}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true
        });
        // 获取状态栏的参数


        // 表格重载 用于搜索
        form.on('submit(submit)', function(data){
            var field=data.field;
            field.name = name
            if($(data.elem).html()=="重置"){
                $("input").val("");
                location.reload();
            }
            table.reload('all', {
                url: '/admin/Sign/findSignApi',
                where: {field}
            });
            return false;
        });

        //头工具栏事件
        table.on('toolbar(all)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            // 获取所有选中的记录
            var data = checkStatus.data;
            // 定义一个空的数组
            var arr = [];
            // 组成一个所有id的数组

            for( let i of data){
                arr.push(i.sign_id)
            }
            var post_data = JSON.stringify(arr)
            switch(obj.event){
                case 'disableSign':
                    $.ajax({
                        type: "POST",
                        dataType: 'json',
                        url: "/admin/Sign/disableSign",
                        data: {'ids':post_data},
                        error:function(error){
                            layer.msg('禁用失败');
                            layer.close(index);
                        },success: function (data) {
                            layer.msg('禁用成功');
                            setTimeout(function () {
                                location.reload();
                            }, 2000)
                        }
                    })
                    break;
                case 'noDisableSign':
                    $.ajax({
                        type: "POST",
                        dataType: 'json',
                        url: "/admin/Sign/noDisableSign",
                        data: {'ids':post_data},
                        error:function(error){
                            layer.msg('禁用失败');
                        },success: function (data) {
                            layer.msg('禁用成功');
                            setTimeout(function () {
                                location.reload();
                            }, 2000)
                        }
                    })
                    break;
            };
        });

        table.on('tool(all)', function(obj){
            var id = obj.data.sign_id
            var sign = obj.data.sign
            var nameData = getQueryVariable('name')
            // 编辑账号
            if(obj.event == 'edit'){
                window.location.href= ("/admin/Sign/updateSign?id="+id + '&callback='+ url);
            }
            if(obj.event == 'pop'){
                window.location.href = "/admin/Sign/getSignPop?model=Popularize" + '&name=' + nameData + '&sign=' + sign;
            }
            if(obj.event == 'reg'){
                console.log('reg')
                window.location.href = "/admin/Sign/getSignReg?model=Registered"  + '&name=' + nameData + '&sign=' + sign;
            }
        });
        $('#all').on('click', function(){
            layer.msg('hello');
        });
    });

</script>

{/block}